// 2025/10/11: 首个版本

using System;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using ASEva;
using ASEva.UIAvalonia;
using ASEva.Utility;

namespace CommonSource
{
    partial class ClientAudioPlayer : DialogPanel
    {
        public ClientAudioPlayer()
        {
            InitializeComponent();
            SetResizableMode(400, 100, 400, 100);
            language = new LanguageSwitch(Resources, AgencyLocal.GetAppLanguage());
            Title = language["title"];
            DataContext = model;

            if (FuncManager.Run("System.GetDesignerModeFlag") == null)
            {
                foreach (var driver in AgencyLocal.GetAudioDrivers())
                {
                    model.Drivers.Add(new DriverItem(driver));
                }
                if (model.Drivers.Count > 0) model.SelectedDriver = model.Drivers[0];
            }
        }

        public String IconResourceName
        {
            set => Icon = ResourceLoader.Load(value)?.ToAvaloniaWindowIcon();
        }

        private class DriverItem(AudioDriverInfo driver)
        {
            public string DriverID => driver.DriverID;
            public override bool Equals(object obj) => driver.DriverID == ((DriverItem)obj).DriverID;
            public override int GetHashCode() => driver.DriverID.GetHashCode();
            public override string ToString() => driver.DriverName;
        }

        private class DeviceItem(AudioDeviceInfo device)
        {
            public string DeviceID => device.DeviceID;
            public override bool Equals(object obj) => device.DeviceID == ((DeviceItem)obj).DeviceID;
            public override int GetHashCode() => device.DeviceID.GetHashCode();
            public override string ToString() => device.DeviceName;
        }

        private partial class Model : ObservableObject
        {
            [ObservableProperty]
            private ObservableCollection<DriverItem> drivers = [];

            [ObservableProperty]
            private ObservableCollection<DeviceItem> devices = [];

            [ObservableProperty]
            private DriverItem selectedDriver;

            [ObservableProperty]
            private DeviceItem selectedDevice;

            partial void OnSelectedDriverChanged(DriverItem value)
            {
                AudioDeviceInfo[] devices = null;
                if (SelectedDriver != null)
                {
                    devices = AgencyLocal.GetAudioReplayDevices(SelectedDriver.DriverID);
                }

                if (devices == null || devices.Length == 0)
                {
                    SelectedDevice = null;
                    Devices.Clear();
                }
                else
                {
                    foreach (var device in devices)
                    {
                        Devices.Add(new DeviceItem(device));
                    }
                    if (Devices.Count > 0) SelectedDevice = Devices[0];
                }
            }

            partial void OnSelectedDeviceChanged(DeviceItem value)
            {
                AgencyLocal.SetAudioReplayDevice(SelectedDriver?.DriverID, SelectedDevice?.DeviceID);
            }
        };

        private LanguageSwitch language;
        private Model model = new();
    }
}